package com.example.word.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.dict.R;
import com.example.word.bean.Browse;
import com.example.word.bean.Dict;
import com.example.word.bean.Unit;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * 词库单元适配器
 */

public class UnitAdapter extends RecyclerView.Adapter<UnitAdapter.ViewHolder> {
    private List<Unit> list = new ArrayList<>();
    private Context mActivity;
    private ItemListener mItemListener;
    private LinearLayout llEmpty;
    private RecyclerView rvNewsList;

    public void setItemListener(ItemListener itemListener) {
        this.mItemListener = itemListener;
    }

    public UnitAdapter(LinearLayout llEmpty, RecyclerView rvNewsList) {
        this.llEmpty = llEmpty;
        this.rvNewsList = rvNewsList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mActivity = viewGroup.getContext();
        View view = LayoutInflater.from(mActivity).inflate(R.layout.item_rv_unit_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Unit unit = list.get(i);

        viewHolder.title.setText("第" + unit.getUnitNumber() + "单元");
        viewHolder.studyCount.setText(String.format("%s / %s", unit.getStudyCount(), unit.getSize()));
        viewHolder.wordCount.setText(String.format("单词数量%s个", unit.getSize()));

        viewHolder.startStudy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemListener != null) {
                    mItemListener.ItemClick(unit);
                }
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemListener != null) {
                    mItemListener.ItemClick(unit);
                }
            }
        });

    }

    public void addItem(List<Unit> listAdd) {
        //如果是加载第一页，需要先清空数据列表
        this.list.clear();
        if (listAdd != null) {
            //添加数据
            this.list.addAll(listAdd);
        }
        //通知RecyclerView进行改变--整体
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView studyCount, wordCount;
        private Button startStudy;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            studyCount = itemView.findViewById(R.id.studyCount);
            wordCount = itemView.findViewById(R.id.wordCount);
            startStudy = itemView.findViewById(R.id.startStudy);

            if (!com.example.word.ui.activity.MainActivity.colorChange) {
                startStudy.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_border_fill));
            } else {
                startStudy.setBackground(mActivity.getDrawable(R.drawable.btn_border_fill_my_color));
            }
        }
    }

    public interface ItemListener {
        void ItemClick(Unit unit);
    }
}
